﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Hockey.Shared;
using Hockey.UILayer.Forms;
using Microsoft.Practices.Prism.Events;
using MVVMDialogs;
using Hockey.UILayer.ViewModels;

namespace Hockey.UILayer.Views
{
    /// <summary>
    /// Interaction logic for PlayerLabel.xaml
    /// </summary>
    public partial class PlayerLabel : UserControl    
    {
        private IBoard _board;
        private IEventAggregator _events;

        private LinearGradientBrush _brushOnMove;
        private LinearGradientBrush _brushNotOnMove;

        /// <summary>
        /// Constructor
        /// </summary>
        public PlayerLabel(IEventAggregator events, IBoard board, PlayerLabelVM vm)
        {
            InitializeComponent();
            this.DataContext = vm;

            if (events == null)
                throw new ArgumentNullException("Events");
            _events = events;

            if (board == null)
                throw new ArgumentNullException("Board");
            _board = board;

            // create brushes
            _brushOnMove = new LinearGradientBrush();
            _brushOnMove.GradientStops.Add(new GradientStop(Colors.White, 0));
            _brushOnMove.GradientStops.Add(new GradientStop(Colors.LightGreen, 1));
            _brushOnMove.Freeze();

            _brushNotOnMove = new LinearGradientBrush();
            _brushNotOnMove.GradientStops.Add(new GradientStop(Colors.White, 0));
            _brushNotOnMove.GradientStops.Add(new GradientStop(Colors.OrangeRed, 1));
            _brushNotOnMove.Freeze();


            // register events            
            this.Loaded += PlayerLabel_Loaded;

            // register notifications
            _events.GetEvent<BoardChanged>().Subscribe((obj) => { Update(); }, true);

        }

        /// <summary>
        /// Setup handler when loaded
        /// </summary>
        void PlayerLabel_Loaded(object sender, RoutedEventArgs e)
        {
            // Not best solution to have tough coupling to the viewmodel
            (this.DataContext as PlayerLabelVM).PuckDirection = PuckDirection;


            // set the arrow transform
            if (PuckDirection == ePuckDir.Down)
            {
                uxArrow.RenderTransform = new RotateTransform(180);
            }

            Update();
        }

        /// <summary>
        /// Direction of puck
        /// </summary>
        public ePuckDir PuckDirection
        { get; set; }

        /// <summary>
        /// Updates the UI
        /// </summary>
        private void Update()
        {
            uxArrow.Fill = _board.PuckDirection == PuckDirection ? Brushes.Green: Brushes.OrangeRed;
            uxBorder.Background = _board.PuckDirection == PuckDirection ? _brushOnMove : _brushNotOnMove;            
        }

        /// <summary>
        /// By mouse click notifies view model to enter player's name
        /// </summary>
        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            // check the point inside the control
            Point mousePoint = e.GetPosition(this);
            if (mousePoint.X > 0 && mousePoint.Y > 0 &&
                mousePoint.X < RenderSize.Width && mousePoint.Y < RenderSize.Height)
            {
                // send notification
                _events.GetEvent<EnterPlayerName>().Publish(this);
                e.Handled = true;
            }


            base.OnMouseLeftButtonDown(e);
        }

    }
}
